﻿using NPOI.Util;
using OSSUtility;
using OSSUtility.Models;
using Ozone.BLL;
using Ozone.Device;
using Ozone.Models;
using Rubyer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static Ozone.Models.KeyPoint;

namespace Ozone.Views.SystemCenter.AdvancedParameter
{
    /// <summary>
    /// RobotByCentrifugalMachine.xaml 的交互逻辑   离心机旁边机械臂
    /// </summary>
    public partial class RobotByCentrifugalMachine : UserControl
    {
        public RobotByCentrifugalMachine()
        {
            InitializeComponent();
        }




        #region  机械臂操作
        /// <summary>
        /// 前后左右微调机械臂
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnVolumeUp_click(object sender, RoutedEventArgs e)
        {

            double dStep = Utility.str2double(txt_Step.Text);
            int nSpeed = Utility.str2int(txt_Speed.Text);
            if (nSpeed < 10 || nSpeed > 100)
                nSpeed = 10;

            double dStepRoate = Utility.str2double(txt_StepRoate.Text);
            int nSpeedRoate = Utility.str2int(txt_SpeedRoate.Text);
            if (nSpeedRoate < 10 || nSpeedRoate > 300)
                nSpeedRoate = 10;

            PointXYZ po = DevRobotByCentrifugalMachine.devRebot.GetCurPos();
            if (po == null)
            {
                return;
            }
            txt_CurPos.Text = po.X.ToString() + "," + po.Y.ToString() + "," + po.Z.ToString();

            string tag = ((Button)sender).Tag.ToString();
            if (tag == "x+")
            {
                po.X += dStep;
            }
            if (tag == "x-")
            {
                po.X -= dStep;
            }


            if (tag == "y+")
            {
                po.Y += dStep;
            }
            if (tag == "y-")
            {
                po.Y -= dStep;
            }


            if (tag == "z+")
            {
                po.Z += dStep;
            }
            if (tag == "z-")
            {
                po.Z -= dStep;
            }

            //旋转
            if (tag == "a+")
            {
                po.A += dStepRoate;
            }
            if (tag == "a-")
            {
                po.A -= dStepRoate;
            }

            if (tag == "b+")
            {
                po.B += dStepRoate;
            }
            if (tag == "b-")
            {
                po.B -= dStepRoate;
            }

            if (tag == "c+")
            {
                po.C += dStepRoate;
            }
            if (tag == "c-")
            {
                po.C -= dStepRoate;
            }

            bool b = DevRobotByCentrifugalMachine.devRebot.MoveTo(po, nSpeed, nSpeedRoate, 1, 1000 * 100);

            if (!b)
                MessageBox.Show("移动失败");


        }








        /// <summary>
        /// 赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetPos_click(object sender, RoutedEventArgs e)
        {
            string str = txt_CurPos.Text;
            string[] arPos = str.Split(',');
            if (arPos.Length == 6)
            {
                txt_X.Text = arPos[0];
                txt_Y.Text = arPos[1];
                txt_Z.Text = arPos[2];
                txt_A.Text = arPos[3];
                txt_B.Text = arPos[4];
                txt_C.Text = arPos[5];
            }
        }

        int nWaitTime = 1000 * 1000;







        private void btnBeginPos_click(object sender, RoutedEventArgs e)
        {

            PointXYZ po = DevRobotByCentrifugalMachine.devRebot.GetCurPos();
            if (po == null)
                txt_CurPos.Text = "获取失败";
            else
                txt_CurPos.Text = po.X.ToString() + "," + po.Y.ToString() + "," + po.Z.ToString() +
                    "," + po.A.ToString() + "," + po.B.ToString() + "," + po.C.ToString();


        }
        public static List<string> RebotTrajectoryLocationList = new List<string>();
        /// <summary>
        /// 添加到轨迹列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddTrajectoryList_click(object sender, RoutedEventArgs e)
        {

            if (string.IsNullOrEmpty(txt_CurPos.Text))
            {
                this.txt_CurPos.Text = "先获取当前点位,在添加到运行轨迹中！";
            }
            else
            {
                string[] ArryLoacation = this.txt_CurPos.Text.Split(',');
                if (ArryLoacation.Length != 6)
                {
                    this.txt_CurPos.Text = "先获取当前点位,在添加到运行轨迹中！";
                }
            }
            RebotTrajectoryLocationList.Add(this.txt_CurPos.Text);

            string RebotTrajectoryMessage = string.Empty;
            if (RebotTrajectoryLocationList != null && RebotTrajectoryLocationList.Count > 0)
            {
                foreach (string str in RebotTrajectoryLocationList) { RebotTrajectoryMessage += str + "|" + "\r\n"; }
            }
            this.RebotTrajectoryList.Text = RebotTrajectoryMessage;
        }


        /// <summary>
        /// 顺序执行点位信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShunxuPos_click(object sender, RoutedEventArgs e)
        {
            double dStep = Utility.str2double(txt_Step.Text);
            int nSpeed = Utility.str2int(txt_Speed.Text);
            if (nSpeed < 10 || nSpeed > 100)
                nSpeed = 10;

            double dStepRoate = Utility.str2double(txt_StepRoate.Text);
            int nSpeedRoate = Utility.str2int(txt_SpeedRoate.Text);
            if (nSpeedRoate < 10 || nSpeedRoate > 300)
                nSpeedRoate = 10;




            if (!string.IsNullOrEmpty(this.RebotTrajectoryList.Text))
            {
                string Po = this.RebotTrajectoryList.Text;


                string[] RebotTrajectoryLocationArry = Po.Split('|');



                if (RebotTrajectoryLocationArry != null && RebotTrajectoryLocationArry.Length > 0)
                {


                    for (int i = 0; i < RebotTrajectoryLocationArry.Length; i++)
                    {

                        PointXYZ po = new PointXYZ();
                        string[] ArryLoacation = RebotTrajectoryLocationArry[i].Split(',');
                        if (ArryLoacation.Length == 6)
                        {
                            po.X = Convert.ToDouble(ArryLoacation[0]);
                            po.Y = Convert.ToDouble(ArryLoacation[1]);
                            po.Z = Convert.ToDouble(ArryLoacation[2]);
                            po.A = Convert.ToDouble(ArryLoacation[3]);
                            po.B = Convert.ToDouble(ArryLoacation[4]);
                            po.C = Convert.ToDouble(ArryLoacation[5]);
                            if (ArryLoacation.Length == 6)
                            {
                                bool b = DevRobotByCentrifugalMachine.devRebot.MoveTo(po, nSpeed, nSpeedRoate, 1, 1000 * 100);
                                if (!b)
                                {
                                    MessageBox.Show("移动失败");
                                }
                            }
                        }
                    }


                }





            }

        }


        /// <summary>
        /// 倒序执行点位信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnDaoxuPos_click(object sender, RoutedEventArgs e)
        {

            double dStep = Utility.str2double(txt_Step.Text);
            int nSpeed = Utility.str2int(txt_Speed.Text);
            if (nSpeed < 10 || nSpeed > 100)
                nSpeed = 10;

            double dStepRoate = Utility.str2double(txt_StepRoate.Text);
            int nSpeedRoate = Utility.str2int(txt_SpeedRoate.Text);
            if (nSpeedRoate < 10 || nSpeedRoate > 300)
                nSpeedRoate = 10;


            if (!string.IsNullOrEmpty(this.RebotTrajectoryList.Text))
            {
                string Po = this.RebotTrajectoryList.Text;


                string[] RebotTrajectoryLocationArry = Po.Split('|');



                if (RebotTrajectoryLocationArry != null && RebotTrajectoryLocationArry.Length > 0)
                {
                    for (int i = RebotTrajectoryLocationArry.Length - 1; i >= 0; i--)
                    {
                        PointXYZ po = new PointXYZ();
                        string[] ArryLoacation = RebotTrajectoryLocationArry[i].Split(',');
                        if (ArryLoacation.Length == 6)
                        {
                            po.X = Convert.ToDouble(ArryLoacation[0]);
                            po.Y = Convert.ToDouble(ArryLoacation[1]);
                            po.Z = Convert.ToDouble(ArryLoacation[2]);
                            po.A = Convert.ToDouble(ArryLoacation[3]);
                            po.B = Convert.ToDouble(ArryLoacation[4]);
                            po.C = Convert.ToDouble(ArryLoacation[5]);
                            if (ArryLoacation.Length == 6)
                            {
                                bool b = DevRobotByCentrifugalMachine.devRebot.MoveTo(po, nSpeed, nSpeedRoate, 6, 1000 * 100);
                                if (!b)
                                {
                                    MessageBox.Show("移动失败");
                                }
                            }
                        }
                    }
                }
            }
        }





        /// <summary>
        /// 夹爪开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnJawOpen_click(object sender, RoutedEventArgs e)
        {



        }

        //夹爪关
        private void btnJawClose_click(object sender, RoutedEventArgs e)
        {
        }



        #endregion















        /// <summary>
        /// 复位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnJawfuwei_click(object sender, RoutedEventArgs e)
        {
            string Message = "即将执行机械臂轴复位速度50,复位过程中需观察周围是否存在障碍物并且准备随时急停，是否继续执行？";
            MessageBoxResult result = MessageBox.Show(Message, "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                ResetPos(20, 20, 0);
            }


        }


        /// <summary>
        /// 机械臂复位
        /// </summary>
        /// <param name="nSpeed"></param>
        /// <param name="nRoateSpeed"></param>
        /// <param name="nType"></param>
        public static void ResetPos(int nSpeed, int nRoateSpeed, int nType = 0)
        {
            List<PointXYZ> lstPoints = new List<PointXYZ>();

            PointXYZ po = DevRobotByCentrifugalMachine.devRebot.GetCurPos();
            if (po == null)
            {
                return;
            }
        }



        /// <summary>
        /// 放个没瓶盖的离心管放个离心管帽关上瓶盖放入离心机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnToLXJ_click(object sender, RoutedEventArgs e)
        {

            //离心管和离心管帽使用一样的编号
            int Number = Convert.ToInt32(txt_PutSGnumber.Text);
            int ZhuanZhiNumber = Convert.ToInt32(txt_ZhuanZhiNumber.Text);

            string Message = "当前会执行,取编号为：" + Number + "的不带离心管帽的离心管放置横抓，离心管帽放置横抓,盖上瓶盖放置离心机转子：" + ZhuanZhiNumber + "号孔位,是否继续执行？";
            MessageBoxResult result = MessageBox.Show(Message, "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
              
            }
            else
            {
                // 用户点击"否"按钮执行的代码
            }

        }


        /// <summary>
        /// 离心机取试管放置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetLXSGToTP_click(object sender, RoutedEventArgs e)
        {

            //离心管和离心管帽使用一样的编号
            int Number = Convert.ToInt32(txt_PutSGnumber.Text);
            int ZhuanZhiNumber = Convert.ToInt32(txt_ZhuanZhiNumber.Text);

            string Message = "当前会执行,取转子号为：" + ZhuanZhiNumber + "的离心管放置在编号为：" + Number + "号的托盘上,是否继续执行？";
            MessageBoxResult result = MessageBox.Show(Message, "确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
               

            }
            else
            {
                // 用户点击"否"按钮执行的代码
            }

        }





        /// <summary>
        /// 试管架取试管 放在三工位上 开盖倒上清夜
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSGToHZ_click(object sender, RoutedEventArgs e)
        {




        }

        /// <summary>
        /// 夹其试管倒入250ML试剂瓶
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDao250ML_click(object sender, RoutedEventArgs e)
        {

      

        }



        /// <summary>
        /// 盖上瓶盖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGSPG_click(object sender, RoutedEventArgs e)
        {
   
        }

        /// <summary>
        /// 取瓶盖放置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSGMToHZ_click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 插管
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInsertPipe_click(object sender, RoutedEventArgs e)
        {

        }


        /// <summary>
        /// 夹爪关
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnJawCloseBig_click(object sender, RoutedEventArgs e)
        {
           
        }

        /// <summary>
        /// 夹爪开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnJawOpenBig_click(object sender, RoutedEventArgs e)
        {
           
        }



        /// <summary>
        /// 第一次执行全流程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQLC1_click(object sender, RoutedEventArgs e)
        {





        }

        /// <summary>
        /// 第二次执行全流程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQLC2_click(object sender, RoutedEventArgs e)
        {



        }


      
    }
}